package me.mysearch;

public class BinarySearchAlter {
    public static void main(String[] args) {
        int[] arr = {1, 5, 6, 7, 8, 9};
        int result = binarySearchAlternative(arr, 8);
        System.out.println(result);
    }

    //改进版
    //左闭右开
    public static int binarySearchAlternative(int[] arr, int target) {
        //j只作为边界，不参与比较
        int i = 0, j = arr.length;
        while (i < j) {
            int m = (i + j) >>> 1;
            if (arr[m] < target) {
                i = m + 1;
            } else if (target < arr[m]) {
                //j不能等于m
                j = m;
            } else {
                return m;
            }
        }
        return -1;
    }
}
